# coding=UTF-8
import os
from utils import read_config, macd_util_v1, date_util, json_util, excel_util
from service import data_service

base_path = read_config.base_path
config_path = os.path.join(read_config.data_path, 'common', 'config')


# 月 macd 上穿0轴
def month_cross():
    result_path = os.path.join(read_config.data_path, 'result', '月macd', date_util.find_last_valid_day(), '跑批结果')
    content_arr = [['编码', '名称', '前月macd', '昨月macd', '本月macd']]
    if not os.path.exists(result_path):
        s_dict = data_service.new_stock_all()
        for code in s_dict:
            macd_list = macd_util_v1.get_macd_list(code, date_util.get_date_str(), 3, 30)
            macd_info0 = json_util.to_json(macd_list[0])
            macd_info1 = json_util.to_json(macd_list[1])
            macd_info2 = json_util.to_json(macd_list[2])
            macd0 = macd_info0['macd']
            macd1 = macd_info1['macd']
            macd2 = macd_info2['macd']
            if macd0 > 0 and macd1 > 0:
                continue
            if (macd0 >= 0 and macd1 < 0) or (macd2 < macd1 < macd0 < 0 and macd0 > -0.01):
                row_info = []
                row_info.append(code)
                row_info.append(s_dict[code]['name'])
                row_info.append(macd2)
                row_info.append(macd1)
                row_info.append(macd0)
                content_arr.append(row_info)
        excel_util.gen_an_excel(result_path, '月macd跑批', '', content_arr)
    if len(content_arr) == 1:
        content_arr = excel_util.get_excel_info(os.path.join(result_path, '月macd跑批.xlsx'), '')
    return content_arr


if __name__ == '__main__':
    month_cross()
